 /*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 /**
 * Project  : EasyAvatar-Android
 * Package  : net.solosky.easyavatar.view
 * File     : FriendExpandableListAdapter.java
 * Author   : solosky < solosky772@qq.com >
 * Created  : Nov 13, 2011
 * License  : Apache License 2.0 
 */
package net.solosky.easyavatar.view;

import net.solosky.easyavatar.R;
import net.solosky.easyavatar.api.Friend;
import net.solosky.easyavatar.api.Group;
import net.solosky.easyavatar.bean.Contact;
import net.solosky.easyavatar.bean.Match;
import net.solosky.easyavatar.bean.Match.MatchType;
import net.solosky.easyavatar.utils.MatchHelper;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

/**
 *
 *
 * @author solosky <solosky772@qq.com>
 *
 */
public class MatchExpandableListAdapter extends BaseExpandableListAdapter  {
	private MatchHelper mMatchHelper;
	private Context     mContext;
	private LayoutInflater mInflater;
	
	public MatchExpandableListAdapter(Context context) {
		this.mContext = context;
		this.mInflater = LayoutInflater.from(context);
	}

	public void setMatchHelper(MatchHelper mMatchHelper) {
		this.mMatchHelper = mMatchHelper;
	}
	
	@Override
	public Object getChild(int groupPosition, int childPosition)  {
		return mMatchHelper.getMatch(mMatchHelper.getGroup(groupPosition), childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,  
            boolean isLastChild, View convertView, ViewGroup parent)  {
		MatchViewHolder holder = null;
		if (convertView == null) {
			holder = new MatchViewHolder();
			holder.contentView = mInflater.inflate(R.layout.li_match_item, null);
			holder.tvFriendName = (TextView) holder.contentView.findViewById(R.id.tv_friend_name);
			holder.ivFriendAvatar = (ImageView) holder.contentView.findViewById(R.id.iv_friend_avatar);
			holder.tvContactName = (TextView) holder.contentView.findViewById(R.id.tv_contact_name);
			holder.ivContactAvatar = (ImageView) holder.contentView.findViewById(R.id.iv_contact_avatar);
			holder.blMatchSatus = (View) holder.contentView.findViewById(R.id.bl_match_status);
			holder.tvMatchStatus = (TextView) holder.contentView.findViewById(R.id.tv_match_status_info);
			holder.blMatchedContact = (View) holder.contentView.findViewById(R.id.bl_matched_contact);
			
			holder.contentView.setTag(holder);
		}else {
			holder = (MatchViewHolder) convertView.getTag();
		}
		
		Match match  = (Match)  getChild(groupPosition, childPosition);
		Friend friend = match.getFriend();
		holder.tvFriendName.setText(friend.getDisplay());
		if(friend.getAvatar().getTag()!=null){
			holder.ivFriendAvatar.setImageBitmap((Bitmap) friend.getAvatar().getTag());
		}else{
			holder.ivFriendAvatar.setImageResource(R.drawable.default_avatar);
		}
		
		Contact contact = match.getContact();
		if(contact!=null){
			holder.blMatchSatus.setVisibility(View.VISIBLE);
			holder.blMatchedContact.setVisibility(View.VISIBLE);
			
			int matchTypeStrId = match.getType()==MatchType.SYSTEM ? R.string.matched_by_system : R.string.matched_by_user;
			holder.tvMatchStatus.setText(mContext.getResources().getString(matchTypeStrId));
			
			holder.tvContactName.setText(contact.getName());
			if(contact.getHead()!=null){
				holder.ivContactAvatar.setImageBitmap(contact.getHead());
			}else{
				holder.ivContactAvatar.setImageResource(R.drawable.default_avatar);
			}
		}else{
			holder.blMatchSatus.setVisibility(View.INVISIBLE);
			holder.blMatchedContact.setVisibility(View.INVISIBLE);
		}
		
		return holder.contentView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return mMatchHelper.getMatchCount(mMatchHelper.getGroup(groupPosition));
	}

	@Override
	public Object getGroup(int groupPosition) {
		return mMatchHelper.getGroup(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return mMatchHelper.getGroupCount();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		GroupViewHolder holder = null;
		if (convertView == null) {
			holder = new GroupViewHolder();
			holder.contentView = mInflater.inflate(R.layout.li_match_group, null);
			holder.tvTitle = (TextView) holder.contentView.findViewById(R.id.tv_title);
			holder.ivIcon  = (ImageView) holder.contentView.findViewById(R.id.iv_icon);
			holder.contentView.setTag(holder);
		}else {
			holder = (GroupViewHolder) convertView.getTag();
		}
		
		Group group = (Group) getGroup(groupPosition);
		holder.tvTitle.setText(String.format("%s (%d)", group.getName(), getChildrenCount(groupPosition)));
		holder.ivIcon.setImageResource(isExpanded?R.drawable.mm_submenu_down_normal:R.drawable.mm_submenu_normal);
		return holder.contentView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
	
	
	private class GroupViewHolder{
		public View contentView;
		public TextView tvTitle;
		public ImageView ivIcon;
	}
	
	private class MatchViewHolder{
		public View contentView;
		public TextView tvFriendName;
		public ImageView ivFriendAvatar;
		public TextView tvContactName;
		public ImageView ivContactAvatar;
		public View blMatchSatus;
		public TextView tvMatchStatus;
		public View blMatchedContact;
		public View blSelectContact;
	}
}
